<template>
    <el-dialog
            title="注册模型"
            :visible.sync="registerVisible"
            :close-on-click-modal="false"
            width="40%"
            style="top: -12%;"
            v-dialogDrag>
        <el-upload
                id="uploadData"
                action=""
                multiple
                :on-change="handleChange"
                :on-remove="handleRemove"
                :limit="4"
                :before-remove="beforeRemove"
                :on-exceed="handleExceed"
                :auto-upload="false"
                :file-list="fileList">
            <!--:show-file-list="false"-->
            <el-button size="small" type="primary" style="margin: 10px 0 20px 0">选取数据</el-button>
        </el-upload>
        <div style="margin-top: 20px;">
            <el-button @click="submitFile" type="primary">确定</el-button>
            <el-button @click="cancel" type="primary">取消</el-button>
        </div>
    </el-dialog>

</template>

<script>

    export default {
        name: "registerDialog",
        data() {
            return {
                registerVisible: false,
                fileList: [],
            }
        },
        props: {},
        computed: {},
        methods: {
            handleRemove(file, fileList) {
                console.log(file, fileList);
            },
            beforeRemove(file, fileList) {
                return this.$confirm(`确定移除 ${ file.name }？`);
            },

            handleChange(file, fileList) {
                this.fileList = fileList;
            },
            handleExceed(files, fileList) {
                this.$message.warning(`当前限制选择 4个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);

            },
            submitFile() {
                let hasXml = false;
                let hasJar = false;
                let hasZip = false;
                let hasPic = false;
                let hasPy = false;
                for (let file of this.fileList) {
                    let fileType = file.name.match(/\.(\S*)/)[1];
                    console.log(fileType);
                    switch (fileType) {
                        case 'xml':
                            hasXml = true;
                            break;
                        case 'jar':
                            hasJar = true;
                            break;
                        case 'png' || 'jpg':
                            hasPic = true;
                            break;
                        case 'zip':
                            hasZip = true;
                            break;
                        case 'py':
                            hasPy = true;
                            break;
                    }
                }
                if ((hasPy || hasJar) && hasXml) {
                    let that = this;
                    let fileData = new FormData();
                    for (let upData of this.fileList) {
                        fileData.append('file', upData.raw);
                    }
                    this.registerVisible = false;
                    // this.$message({
                    //     type: 'info',
                    //     message: '请稍候……',
                    //     duration: 1000
                    // });
                    // let url = 'http://192.168.1.5:9000/dbfg/parallel/register';
                    this.$axios.post(this.$URL.parallelRegisterUrl, fileData)
                        .then(function (response) {
                            that.load = false;
                            if (response.code === 200) {
                                // that.$emit('childRefresh');
                                that.$message({
                                    type: 'success',
                                    message: '模型注册成功!'
                                });
                                that.$emit('refresh');
                            } else {
                                that.$message.error('模型注册失败!');
                            }
                            //判断是否注册成功进行弹窗
                        }).catch(function (error) {
                        that.load = false;
                        that.$message.error('模型注册失败!');
                    });
                    this.fileList = [];
                } else {
                    this.$message.error('请检查上传文件是否正确');
                }
            },
            cancel() {
                this.registerVisible = false;
                this.fileList = [];
            }
        },
        created() {
            let that = this;
            this.$Bus.$on('registerVisible', params => {
                that.registerVisible = params.visible;
            });

        }

    }
</script>

<style>
    .step {
        margin: 20px
    }

    .el-input-number {
        width: 100% !important
    }


</style>